package com.wyz.activemq.queue;

import com.wyz.activemq.config.AppConfig;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @Author: wyz
 * @Date: 2019/4/12 19:32
 * @Description: 演示activeMQ 队列模式之生产者
 */
public class Producer {


    public static void main(String[] args) throws JMSException {
        // 1.创建ActiveMQFactory
       ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(AppConfig.ACTIVEMQ_URL);
        //2.创建连接
        Connection connection = factory.createConnection();

        connection.start();
        //3.创建回话 第一个参数是是否开启事务 第二个参数是签收机制  AUTO_ACKNOWLEDGE 表示自动签收
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //4.创建队列
        Queue queue = session.createQueue(AppConfig.QUEUE_NAME);
        //5.创建生产者(绑定队列)
        MessageProducer producer = session.createProducer(queue);
        for (int i = 0; i < 10; i++) {
            //创建发送的消息
            TextMessage textMessage = session.createTextMessage("第" + i + "次，发送消息====" + i);
            //发送消息
            producer.send(textMessage);
        }
        System.out.println("消息发送完成！！！！！");
        //关闭连接
        connection.close();
    }
}
